package com.cqh.recruit.controller.student;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.recruit.pojo.dto.StudentActivityQueryDTO;
import com.cqh.recruit.pojo.vo.StudentActivityListVO;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IActivityService;
import com.cqh.recruit.utils.StudentThreadLocal;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 *
 *  前端控制器
 *
 *
 * @author dh
 * @since 2025-10-18
 * @version v1.0
 */
@RestController("studentActivityController")
@RequestMapping("/api/student/activity")
@Tag(name = "学生端-活动信息表接口")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    /**
     * 根据动态条件分页查询活动列表
     * @return
     */
    @PostMapping("/list")
    @Operation(summary= "根据动态条件分页查询活动列表")
    public Result list(@RequestBody StudentActivityQueryDTO queryDto) {
        Page<StudentActivityListVO> page =activityService.queryActivityList(queryDto);
        return Result.buildSuccess(page.getRecords(), page.getTotal());
    }

    /**
     * 查询当前登录学生已报名的活动列表
     * @return
     */
    @PostMapping("/enrolled")
    @Operation(summary= "查询当前登录学生已报名的活动列表")
    public Result enrolled(@RequestBody StudentActivityQueryDTO queryDto) {
        Page<StudentActivityListVO> page =activityService.queryEnrolledActivityList(queryDto);
        return Result.buildSuccess(page.getRecords(), page.getTotal());
    }

    /**
     * 根据id查询活动详情
     * @return
     */
    @GetMapping("/find")
    @Operation(summary= "根据id查询活动详情")
    public Result find(@RequestParam Long id) {
        return Result.buildSuccess(activityService.getById(id));
    }

    @PostMapping("/applyActivity")
    @Operation(summary= "活动报名")
    public Result applyActivity(Long activityId) {
        Long stuId = StudentThreadLocal.get().getId();
        return activityService.applyActivity(activityId, stuId);
    }

    /**
     * 取消报名
     */
    @DeleteMapping("/cancel")
    public Result cancelApplication(Long activityId) {
        Long stuId = StudentThreadLocal.get().getId();
        if (stuId == null) {
            return Result.buildFail(20001,"用户未登录");
        }
        return activityService.cancelApplication(activityId, stuId);
    }

    /**
     * 获取报名状态
     */
    @GetMapping("/applicationStatus")
    @Operation(summary= "获取活动报名状态")
    public Result getApplicationStatus(@RequestParam Long activityId) {
        Long stuId = StudentThreadLocal.get().getId();
        if (stuId == null) {
            return Result.buildFail(20001,"用户未登录");
        }
        return activityService.getApplicationStatus(activityId, stuId);
    }


}